import { _decorator, AudioClip, Collider, Component, find, Graphics, instantiate, Node, Prefab, Vec3 } from 'cc';
import { Entity } from '../Entity';
import { EnemyIdleState } from './EnemyIdleState';
import { EnemyMoveState } from './EnemyMoveState';
import { EnemyStateMachine } from './EnemyStateMachine';
import { EnemyAttackState } from './EnemyAttackState';
import { EnemyBattleState } from './EnemyBattleState';
import { EnemyDeathState } from './EnemyDeathState';
import { ExpWnd } from '../ExpWnd';
const { ccclass, property } = _decorator;

@ccclass('Enemy')
export class Enemy extends Entity {
    @property(Number)
    public moveSpeed: number = 5;

    @property(Prefab)
    public sunExp: Prefab;

    @property(AudioClip)
    public attackClip: AudioClip = null;

    public player: Node | null = null;

    public attackCooldown: number = 0.1; // 攻击冷却时间
    public lastAttackTime: number = 0;

    public isDead: boolean = false;

    public stateMachine: EnemyStateMachine;
    public idleState: EnemyIdleState;
    public moveState: EnemyMoveState;
    public attackState: EnemyAttackState;
    public battleState: EnemyBattleState;
    public deathState: EnemyDeathState;

    public onLoad() {
        super.onLoad();
        this.stateMachine = new EnemyStateMachine();
        this.idleState = new EnemyIdleState(this, this.stateMachine);
        this.moveState = new EnemyMoveState(this, this.stateMachine);
        this.attackState = new EnemyAttackState(this, this.stateMachine);
        this.battleState = new EnemyBattleState(this, this.stateMachine);
        this.deathState = new EnemyDeathState(this, this.stateMachine);
    }

    protected override start() {
        super.start();
        this.player = find("Canvas/Player");

        this.stateMachine.Initialize(this.idleState);
    }

    protected override update(dt: number) {
        super.update(dt);
        this.stateMachine.currentState.update(dt);
        this.updateFacingDirection();
    }

    // 更新敌人朝向
    private updateFacingDirection(): void {
        if (!this.player || this.isDead) return;

        const playerPos = this.player.position;
        const enemyPos = this.node.position;

        // 如果玩家在敌人左边
        if (playerPos.x < enemyPos.x) {
            this.node.setScale(new Vec3(-1, 1, 1));
        }
        // 如果玩家在敌人右边
        else {
            this.node.setScale(new Vec3(1, 1, 1));
        }
    }


    public TakeDamage(damageToTake: number) {
        this.currentHp -= damageToTake;
        this.hpBar.updateHealth(this.currentHp, this.maxHp);
        if (this.currentHp <= 0) {
            this.isDead = true;
            if (this.isDead) {
                this.stateMachine.changeState(this.deathState);
            }
        }
    }

    /** 检测是否在攻击范围内 */
    public isPlayerInAttackRange(): boolean {
        if (this.isDead) return;
        return this.checkProximity(this.player, this.attackRadius);
    }
    /** 检测是否在追击范围内 */
    public isPlayerInDetectionRange(): boolean {
        if (this.isDead) return;
        return this.checkProximity(this.player, this.detectionRadius);
    }

    public spanSunExp() {
        const sunExp = instantiate(this.sunExp);
        const sunLayer = find("Canvas/SunLayer");
        sunExp.setPosition(this.node.position);
        sunLayer.addChild(sunExp);

    }
}


